-- 47.lua
-- Created by zouyb Jun/26/2015
-- 海盗BUFF光效

return {
    onAppear = function(monster)    
        -- 当前怪物的基类ID
        local baseClassId = monster:query("base_class_id");

        -- 获取其他所有怪物
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;

        -- 是否有酗酒大副
        local needAddBuf = false;

        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                count = count + 1;
            end   
            
            -- 如果酗酒大副在场，需要播放“属性增益”光效
            if monsterGrid.gridData.monster:query("base_class_id") == 61 then
                needAddBuf = true;
            end                   
        end

        if needAddBuf then
            -- 酗酒大副在场，播放“属性增益”光效
            local monsterGrid = UIDungeonMgr:getCurLevel().grids[monster:getPos()];
            playAddBuffEffect(monsterGrid, BUFF_TYPE_ATTACK);
        end

        -- 注册其他怪物死亡时的回调
        if count > 0 then
            UIDungeonMgr:getCurLevel().monsterBuffEffectM:addEventModule("monster47_appear");   
            EventMgr.removeAll("monster47_appear"); 
            EventMgr.register("monster47_appear", event.MONSTER_DIE_LOGIC, function(params)
                local pos = params.pos;
                local grid = DungeonM.getGridByPos(pos);
                if grid.monster then
                    local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
                    for i = 1, #openMonsterGrids do
                        local monsterGrid = openMonsterGrids[i];
                        -- 场上其他怪物死亡时，所有在场海盗播放“属性减益”光效
                        if monsterGrid.gridData.monster ~= grid.monster
                            and monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                            playAddDeBuffEffect(monsterGrid, DEBUFF_TYPE_HP);
                        end
                    end
                end
            end);
        end         
    end,

    onDie = function(monster)   
        -- 当前怪物的基类ID
        local baseClassId = monster:query("base_class_id");

        -- 获取其他所有海盗
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;
        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                count = count + 1;
            end
        end   

        if count == 0 then  
            EventMgr.removeAll("monster47_appear");
        end  
    end,
};